<?php

/**
 *
 * @package    opensocial
 * @subpackage person
 * @author     Daniel Botelho <botelho.daniel@gmail.com>
 */
abstract class OSPerson
{
  const PERSON_CLASS = "opensocial.DataRequest.PersonId";
  
  private $var_name;
  
  //FIXME: Meter todos os padrões como appendable( como está o GET_ID_PATTERN
  const GET_PERSON_REQUEST_PATTERN = "%s.add(%s.newFetchPersonRequest(%s,%s),\"%s\");\n";
  const GET_NAME_PATTERN = "%s.getDisplayName()";
  const GET_ID_PATTERN = "%s.getId()";
  const GET_ISOWNER_PATTERN = "%s.isOwner()";
  const GET_ISVIEWER_PATTERN = "%s.isViewer()";
  const GET_FIELD = "%s.getField(%s)";

  /**
  * Esta classe cria um user em determinado contexto de um pedido ao container, com o seu nome da variável.
  */
  function __construct(/*$container,*/$var)
  {
    //$this->container = $container;
    $this->var_name = $var;
    //$this->pattern = sfConfig::get('app_opensocial_pattern');
  }

  /**
   * @return string This OSPerson variable name
   */
  function getVarName(){ return $this->var_name; }
  //function getContainer(){ return $this->container; }

  abstract function getType();

  //FIXME: o $appendable não está a ser usado...
  function getName($appendable=false,$checkInit=true)
  {
    if($checkInit)
      return StringUtil::appendableString("((".$this->getVarName().")? ".sprintf(OSPerson::GET_NAME_PATTERN,$this->getVarName()).":null)",$appendable);
    else
      return StringUtil::appendableString(sprintf(OSPerson::GET_NAME_PATTERN,$this->getVarName()),$appendable);
  }

  function getId($appendable=false,$checkInit=true)
  {
    if($checkInit)
  	  return StringUtil::appendableString("((".$this->getVarName().")? ".sprintf(OSPerson::GET_ID_PATTERN,$this->getVarName()).":null)",$appendable);
    else
      return StringUtil::appendableString(sprintf(OSPerson::GET_ID_PATTERN,$this->getVarName()),$appendable);
  }

  function isOwner($appendable=false,$checkInit=true)
  {
    if($checkInit){
      return StringUtil::appendableString("(".$this->getVarName()." && ".sprintf(OSPerson::GET_ISOWNER_PATTERN,$this->getVarName()).")",$appendable);
    }else
      return StringUtil::appendableString(sprintf(OSPerson::GET_ISOWNER_PATTERN,$this->getVarName()),$appendable);
  }

  function isViewer($appendable=false)
  {
    return StringUtil::appendableString(sprintf(OSPerson::GET_ISVIEWER_PATTERN,$this->getVarName()),$appendable);
  }
  
  function getField($field,$appendable=false)
  {
  	return StringUtil::appendableString(sprintf(OSPerson::GET_FIELD,$this->getVarName(),$field),$appendable);
  }
  

  function getThumbnailURL($appendable=false,$checkInit=true)
  {
    if($checkInit)
      return StringUtil::appendableString("((".$this->getVarName().")? ".sprintf(OSPerson::GET_FIELD,$this->getVarName(),OSPersonField::THUMBNAIL_URL).":null)",$appendable);
    else
      return StringUtil::appendableString(sprintf(OSPerson::GET_FIELD,$this->getVarName(),OSPersonField::THUMBNAIL_URL),$appendable);
  }

  function getProfileURL($appendable=false,$checkInit=true)
  {
    if($checkInit)
      return StringUtil::appendableString("((".$this->getVarName().")? ".sprintf(OSPerson::GET_FIELD,$this->getVarName(),OSPersonField::PROFILE_URL).":null)",$appendable);
    else
      return StringUtil::appendableString(sprintf(OSPerson::GET_FIELD,$this->getVarName(),OSPersonField::PROFILE_URL),$appendable);
  }

}
